﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using qltscd.Models;
using qltscd.Contansts;

namespace qltscd.Helper {
    public class SessionHelper {
        /// <summary>
        /// set session variable when login
        /// </summary>
        /// <param name="nguoiDung"></param>
        public static void SetUser (DanhMucNguoiDung nguoiDung) {
            if (nguoiDung != null) {
                HttpContext.Current.Session[SessionConstants.IS_USER_LOG_IN] = true;
                HttpContext.Current.Session[SessionConstants.LOG_IN_USER_ID] = nguoiDung.IdentityNguoiDung;
                HttpContext.Current.Session[SessionConstants.LOG_IN_USER_NAME] = nguoiDung.HoTenDem + " " + nguoiDung.TenNguoiDung;
                HttpContext.Current.Session[SessionConstants.LOG_IN_USER_GROUP_NAME] = nguoiDung.DanhMucNhomNguoiDung.TenNhomNguoiDung;
                HttpContext.Current.Session[SessionConstants.LOG_IN_DonViQuanLy] = nguoiDung.DanhMucDonViQuanLy.TenDonVi;
                HttpContext.Current.Session[SessionConstants.QUYEN_NHOM_NGUOI_DUNG] = nguoiDung.DanhMucNhomNguoiDung.Quyen;
            }
        }
        /// <summary>
        /// Clear session information ( usually use when log out)
        /// </summary>
        public static void ClearUser () {
            HttpContext.Current.Session[SessionConstants.IS_USER_LOG_IN] = null;
            HttpContext.Current.Session[SessionConstants.LOG_IN_USER_ID] = null;
            HttpContext.Current.Session[SessionConstants.LOG_IN_USER_NAME] = null;
            HttpContext.Current.Session[SessionConstants.LOG_IN_USER_GROUP_NAME] = null;
            HttpContext.Current.Session[SessionConstants.LOG_IN_DonViQuanLy] = null;
            HttpContext.Current.Session[SessionConstants.QUYEN_NHOM_NGUOI_DUNG] = null;
        }
        /// <summary>
        /// Check if current user is logined
        /// </summary>
        /// <returns></returns>
        public static bool IsLogin () {
            if (HttpContext.Current.Session[SessionConstants.IS_USER_LOG_IN] != null) {
                return (bool)HttpContext.Current.Session[SessionConstants.IS_USER_LOG_IN];
            } else {
                return false;
            }
        }
        /// <summary>
        /// Get current login user id   
        /// </summary>
        public static int? UserId {
            get {
                if (HttpContext.Current.Session[SessionConstants.LOG_IN_USER_ID] != null) {
                    return (int)HttpContext.Current.Session[SessionConstants.LOG_IN_USER_ID];
                } else {
                    return null;
                }
            }
        }
        /// <summary>
        /// Get current login user name
        /// </summary>
        public static string UserName {
            get {
                if (HttpContext.Current.Session[SessionConstants.LOG_IN_USER_NAME] != null) {
                    return (string)HttpContext.Current.Session[SessionConstants.LOG_IN_USER_NAME];
                } else {
                    return string.Empty;
                }
            }
        }

        public static string DonViQuanLy {
            get {
                if (HttpContext.Current.Session[SessionConstants.LOG_IN_DonViQuanLy] != null) {
                    return (string)HttpContext.Current.Session[SessionConstants.LOG_IN_DonViQuanLy];
                } else {
                    return string.Empty;
                }
            }
        }

        public static string NhomNguoiDung{
            get {
                if (HttpContext.Current.Session[SessionConstants.LOG_IN_USER_GROUP_NAME] != null) {
                    return (string)HttpContext.Current.Session[SessionConstants.LOG_IN_USER_GROUP_NAME];
                } else {
                    return string.Empty;
                }
            }
        }
        public static string QuyenNhom {
            get {
                if (HttpContext.Current.Session[qltscd.Contansts.SessionConstants.QUYEN_NHOM_NGUOI_DUNG] == null
                    || HttpContext.Current.Session[qltscd.Contansts.SessionConstants.QUYEN_NHOM_NGUOI_DUNG].ToString() == "")
                    return "000000";
                return HttpContext.Current.Session[qltscd.Contansts.SessionConstants.QUYEN_NHOM_NGUOI_DUNG].ToString();
            }
        }
    }
}